﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Biz;
using Cn.Edwon.Spring;
using Cn.Cellula.Module.Stat.Model;
namespace Cn.Cellula.Website.Admin.Module.Stat {
    public partial class EditSeries : PageSupport {
        IBiz biz = BizFactory.Get<IBiz>();
        String EOKey = "";
        protected void Page_Load(object sender, EventArgs e) {
            EOKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(EOKey)) {
                    FillForm();
                } else {
                    String StatKey = Request.QueryString["sid"];
                    try {
                        ModTStatEO stat = biz.Load<ModTStatEO>(StatKey);
                        this.lab_StatName.Text = stat.StatName;
                        this.hid_StatKey.Value = stat.StatKey;
                    } catch (Exception ex) {
                        log.Error(ex.Message);
                        this.lab_StatName.Text = "统计对象参数错误";
                        this.btn_save.Enabled = false;
                    }
                }
            }
        }

        private void InitForm() {
            IList<ModTStatRenderAsEO> list = biz.LoadAll<ModTStatRenderAsEO>();
            this.ddl_RenderAs.DataValueField = "RenderAsKey";
            this.ddl_RenderAs.DataTextField = "RenderAsName";
            this.ddl_RenderAs.DataSource = list;
            this.ddl_RenderAs.DataBind();
        }

        private void FillForm() {
            ModVStatSeriesEO eo = biz.Load<ModVStatSeriesEO>(EOKey);
            this.hid_StatKey.Value = eo.StatKey;
            this.lab_StatName.Text = eo.StatName;
            this.txt_Color.Text = eo.Color;
            this.txt_SeriesTitle.Text = eo.SeriesTitle;
            this.txt_OrderNumber.Text = eo.OrderNumber + "";
            this.txt_SeriesParam.Text = eo.SeriesParam;
            this.txt_SeriesValue.Text = eo.SeriesValue;
            this.ddl_RenderAs.SelectedValue = eo.RenderAs;
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            if (String.IsNullOrEmpty(EOKey)) {
                ModTStatSeriesEO eo = new ModTStatSeriesEO();
                eo.Color = this.txt_Color.Text;
                eo.SeriesTitle = this.txt_SeriesTitle.Text;
                try {
                    eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
                } catch { }
                eo.StatKey = this.hid_StatKey.Value;
                eo.SeriesParam = this.txt_SeriesParam.Text;
                eo.SeriesValue = this.txt_SeriesValue.Text;
                eo.RenderAs = this.ddl_RenderAs.SelectedValue;
                biz.Save<ModTStatSeriesEO>(eo);
            } else { //update
                ModTStatSeriesEO eo = biz.Load<ModTStatSeriesEO>(EOKey);
                eo.Color = this.txt_Color.Text;
                eo.SeriesTitle = this.txt_SeriesTitle.Text;
                try {
                    eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
                } catch { }
                eo.SeriesParam = this.txt_SeriesParam.Text;
                eo.SeriesValue = this.txt_SeriesValue.Text;
                eo.RenderAs = this.ddl_RenderAs.SelectedValue;
                biz.Update<ModTStatSeriesEO>(eo);
            }
            Response.Redirect("List.aspx?sid=" + this.hid_StatKey.Value);
        }
    }
}